//
//  YCIAPManager.m
//  IAPManager
//
//  Created by 唐可雄 on 2019/3/6.
//  Copyright © 2019年 swustyc. All rights reserved.
//

#import "YCIAPManager.h"

@implementation YCIAPManager

- (instancetype)init {
    self = [super init];
    if (self) {
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    }
    return self;
}

#pragma mark - Products

- (SKProductsRequest *)requestProductsInfo:(NSSet *)productIds {
    if (productIds == nil || productIds.count == 0) {
        return nil;
    }
    
    SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:productIds];
    request.delegate = self;
    [request start];
    
    return request;
}

#pragma mark - SKProductsRequestDelegate

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    if ([self.delegate respondsToSelector:@selector(productsRequest:didReceiveResponse:)]) {
        [self.delegate productsRequest:request didReceiveResponse:response];
    }
}

#pragma mark - Transaction

- (void)purchaseProduct:(SKProduct *)product {
    if ([SKPaymentQueue canMakePayments]) {
        if (product) {
            
        }
    } else {
        if ([self.delegate respondsToSelector:@selector(transactWithResult:)]) {
            [self.delegate transactWithResult:YCIAPResultNotAllow];
        }
    }
}

- (void)resumeTask {
    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
    if (receiptURL) {
        NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
        if (!receipt) {
            
        }
    }
}

#pragma mark - SKPaymentTransactionObserver



@end
